package com.home.security.app.handler;

import com.home.base.core.framework.enums.ResultCodeEnum;
import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.handler.exception.BizException;
import com.home.security.app.exception.ClientTokenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * Description:
 * Created by 王大宸 on 2021-01-18 15:39
 * Created with IntelliJ IDEA.
 */
@RestControllerAdvice(basePackages = {"com.home"})
public class GlobalAppExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalAppExceptionHandler.class);

    /***
     * 功能说明：找不到session,跳转到登录页面
     *
     * @author 王大宸
     * @date 2020/11/10 23:18
     * @param response  response
     * @param ex        ex
     * @return com.home.common.framework.msg.ServerResponse<java.lang.String>
     */
    @ExceptionHandler(ClientTokenException.class)
    public ServerResponse<String> unknownSessionException(HttpServletResponse response, BizException ex) {
        logger.error(ex.getMessage(), ex);
        response.setStatus(HttpStatus.OK.value());
        return ServerResponse.errStatusMsg(ResultCodeEnum.CLIENT_TOKEN_EXCEPTION.getStatus(),
                ResultCodeEnum.CLIENT_TOKEN_EXCEPTION.getMessage());
    }

}
